<?php

/***
 * 描述：商品价格库存表
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: ProductPriceModel.class.php 3 2013-10-14 02:49:41Z whtaok@gmail.com $
 */
class ProductPriceModel extends CommonModel {
	protected $_auto = array (
			array('is_valid','2',1), 	// 系统默认为2，审核通过
			array('is_show','1',1), 	// 系统默认为1，显示
			array('crt_time','time',1,'function'), // 对crt_time字段在更新的时候写入当前时间戳
			array('upd_time','time',3,'function'), // 对upd_time字段在更新的时候写入当前时间戳
			array('crt_user','getSUID',1,'callback'), // 对name字段在新增的时候回调getSUID方法
			array('upd_user','getSUID',3,'callback'), // 对name字段在新增的时候回调getSUID方法			
	);
	protected $_validate = array(
			array('price_real','require','实际价格不能为空！',0),
			array('price_sale','require','推荐价格不能为空！',0),
			array('price_vip','require','会员价不能为空！',0),
			array('shop_id','require','店铺不能为空！',0),
			array('prod_id','require','商品不能为空！',0),
	);
	
	//更改价格
	function updPrice($shop_id,$prod_id,$price_real,$price_sale,$price_vip){
		$flag = $this->check($price_real, 'currency')
		&& $this->check($price_sale, 'currency')
		&& $this->check($price_vip, 'currency')
		&& $this->check($prod_id, 'require') 
		&& $this->check($shop_id, 'require');
		if($flag){
			$where = array('shop_id'=>$shop_id,'prod_id'=>$prod_id);
			$data = array(
				'price_real' => $price_real,
				'price_sale' => $price_sale,
				'price_vip' => $price_vip,
			);
			$flag = $this->where($where)->save($data);
			if($flag){
				$detail = $this->field('shop_id,prod_id,price_real,price_vip,price_sale')->where($where)->find();
				product_log('price',$detail);
				return $detail;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}	
	
	//入库
	function addStockQty($shop_id,$prod_id,$add_stock_qty,$bak='库存添加'){
		$flag = $this->check($add_stock_qty, 'integer') && $this->check($prod_id, 'require') && $this->check($shop_id, 'require');
		if($flag){
			$where = array('shop_id'=>$shop_id,'prod_id'=>$prod_id);
			$flag = $this->where($where)->setInc('stock_qty',$add_stock_qty);
			if($flag){
				$detail = $this->field('shop_id,prod_id,stock_qty')->where($where)->find();
				$detail['add_stock_qty']=$add_stock_qty;
				$detail['bak']=$bak;
				product_log('stock',$detail);
				return $detail;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	//更新库存警告数量
	function updStockWarnQty($shop_id,$prod_id,$stock_warn_qty){
		$flag = $this->check($stock_warn_qty, 'integer') && $this->check($prod_id, 'require') && $this->check($shop_id, 'require');
		if($flag){
			$where = array('shop_id'=>$shop_id,'prod_id'=>$prod_id);
			$flag = $this->where($where)->setField('stock_warn_qty',$stock_warn_qty);
			if($flag){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
}

?>